use ggez::{mint, Context, GameResult};
use ggez::graphics;
use crate::tank::Tank;

pub struct Projectile {
    pub position: (f32, f32),
    velocity: (f32, f32),
    pub damage: f32,
    radius: f32,
}

impl Projectile {
    pub fn new(x: f32, y: f32, vx: f32, vy: f32, damage: f32) -> Self {
        Self {
            position: (x, y),
            velocity: (vx, vy),
            damage,
            radius: 3.0,
        }
    }
    
    pub fn update(&mut self) {
        self.position.0 += self.velocity.0;
        self.position.1 += self.velocity.1;
    }
    
    pub fn check_collision(&self, tank: &Tank) -> bool {
        let dx = self.position.0 - tank.state_data.x;
        let dy = self.position.1 - tank.state_data.y;
        let distance = (dx * dx + dy * dy).sqrt();
        
        distance < self.radius + 10.0 // 10是坦克的近似半径
    }
    
    pub fn draw(&self, canvas: &mut graphics::Canvas, ctx: &mut Context) -> GameResult {
        let point: mint::Point2<f32> = mint::Point2 {
            x: self.position.0,
            y: self.position.1,
        };
        let circle = graphics::Mesh::new_circle(
            ctx,
            graphics::DrawMode::fill(),
            point,
            self.radius,
            0.1,
            graphics::Color::YELLOW,
        )?;
        
        canvas.draw(&circle, graphics::DrawParam::default());
        Ok(())
    }
}